package com.ys.controller.api.finance;

import com.ys.config.MenuId;
import com.ys.entity.finance.TcwOperateCompanyPrepayment;
import com.ys.entity.finance.TcwOperateCompanyPrepaymentFlow;
import com.ys.entity.sys.YsCol;
import com.ys.service.finance.TcwOperateCompanyPrepaymentService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 运营公司预付款管理流水
 */
@Controller
@RequestMapping("/api/inter/tcwOperateCompanyPrepaymentFlow")
public class ApiInterTcwOperateCompanyPrepaymentFlowController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTcwOperateCompanyPrepaymentFlowController.class);

    @Autowired
    private CommonApiService<TcwOperateCompanyPrepaymentFlow> commonApiService;


    private Long menuId = MenuId.TCW_OperateCompanyPrepaymentFlow;


    //权限字符
    private final String PERMISSIONS_VIEW = "tcwOperateCompanyPrepaymentFlow:view";

    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TcwOperateCompanyPrepaymentFlow setSeachSql(TcwOperateCompanyPrepaymentFlow cond){
        cond
                .addLeftJoin("TCW_OperateCompanyPrepayment B ON B.Id = A.OperateCompanyPrepaymentId ")//运营公司预付款管理表
                .addField("B.Vgsbm AS vgsbm")//运营公司编码
                .addField("B.FkVid AS fkVid")//付款账户编码
                .addField("B.FkVyhzh AS fkVyhzh")//付款账号
                .addField("B.FkVkhh AS fkVkhh")//付款开户行
                .addField("B.FkVyhzhmc AS fkVyhzhmc")//付款账户名称
                .addField("B.Vid AS vid")//收款账户编码
                .addField("B.Vyhzh AS vyhzh")//收款账号
                .addField("B.Vkhh AS vkhh")//收款开户行
                .addField("B.Vyhzhmc AS vyhzhmc")//收款账户名称
                .addField("B.OperateCompanyPrepaymentType AS operateCompanyPrepaymentType")//账号类型（0.公司对公账户 | 1.万里汇）

            .addLeftJoin("THR_OperateCompanyApply C ON C.Vgsbm = B.Vgsbm ")
                .addField("C.Vgsjc AS vgsjc")//公司简称
                .addField("C.CUser AS cUser")//用户工号


                .addLeftJoin("TS_UserChange U ON U.UserId = A.UserId AND U.Status = 1 ")
                .addField("U.UserId + '/' + U.UserName AS _ys_createUserName")//流水制单人

        //排序
        .setOrderby("A.Id DESC");

        cond = commonApiService.dataAuth(menuId, cond);
        return  cond;
    }


    /**
     * 查询
     */
    @RequiresPermissions(PERMISSIONS_VIEW)
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
                                 @ModelAttribute TcwOperateCompanyPrepaymentFlow cond,String conditions
    ){
        try {
            cond = setSeachSql(cond);
            //查询条件
            String nameLinked = ConditionUtil.getConditionValue(conditions, "_ys_createUserName");//制单人
            if (nameLinked != null){
                cond.where("U.UserId LIKE '%" + nameLinked + "%'" + " OR  U.UserName LIKE '%" + nameLinked + "%'");
            }

            return commonApiService.searchLay(cond);
        }catch (Exception e){
            logger.error("查询出错：",e.getMessage(),e);
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }
    }

    /**
     * 导出
     */
    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TcwOperateCompanyPrepaymentFlow cond,String conditions){
        try {
            cond.setPage(0).setSize(999999);
            cond = setSeachSql(cond);
            //查询条件
            String nameLinked = ConditionUtil.getConditionValue(conditions, "_ys_createUserName");//制单人
            if (nameLinked != null){
                cond.where("U.UserId LIKE '%" + nameLinked + "%'" + " OR  U.UserName LIKE '%" + nameLinked + "%'");
            }
            //获取菜单字段数据
            List<YsCol> listField = new YsCol()
                    .setMenuId(menuId)
                    .setFrontShow(1)
                    .setOrderby("A.SortNo asc")
                    .queryList();
            //获取数据
            List<Map<String, Object>> listData = cond.queryMap();

            //处理数据
            ExportExcelUtils<TcwOperateCompanyPrepaymentFlow> util = new ExportExcelUtils<>(new TcwOperateCompanyPrepaymentFlow());
            String toFilePath = util.exportExcel(listData, listField, "运营公司预付款管理流水");
            UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
            return R.ok(dto.getUrl());
        }catch (Exception e){
            logger.error("导出出错：",e.getMessage(),e);
            return R.error("导出失败，请稍后重试！");
        }
    }




}